<template>
  <div>
    <el-form ref="passwordForm" :model="passwordUpdate" :rules="passwordRule" label-width="100px"
             style="width: 600px;margin: 0 40% 0 auto">
      <el-form-item label="原密码" prop="password">
        <el-input show-password v-model="passwordUpdate.password"/>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input show-password v-model="passwordUpdate.newPassword"/>
      </el-form-item>
      <el-form-item>
        <div style="margin-left: 50%;transform: translateX(-25%)">
          <el-button type="primary" @click="updateConfirm">保存</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "passwordUpdate",
  data() {
    return {
      passwordUpdate: {
        id: "",
        password: "",
        newPassword: ""
      },
      passwordRule: {
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {pattern: /^[\u0020-\u007e]{4,16}$/, message: "密码的长度必须是4~16位！", trigger: "blur"},
        ],
        newPassword: [
          {required: true, message: "请输入新密码密码", trigger: "blur"},
          {pattern: /^[\u0020-\u007e]{4,16}$/, message: "密码的长度必须是4~16位！", trigger: "blur"},
        ],
      }
    }
  },
  methods: {
    updateConfirm() {
      this.$refs.passwordForm.validate((valid) => {
        if (valid) {
          let url = "http://localhost:9999/zx-ums/user/updatePassword"
          let data = this.qs.stringify(this.passwordUpdate);
          this.axios.create(
              {'headers': {'Authorization': localStorage.getItem('userJwt')}}
          ).post(url, data).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state === 200) {
              this.$message({message: "修改完毕", type: "success"})
            } else {
              this.$message({message: jsonResult.message, type: "error"})
            }
          })
        } else {
          this.$message({message: "数据尚未符合要求", type: "error"})
        }
      })
    }
  },
  mounted() {
    this.passwordUpdate.id = localStorage.getItem("userId");
  }
}
</script>

<style scoped>

</style>